//
//  ComenLoginViewController.m
//  penzer
//
//  Created by caiyc on 2018/11/6.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "ComenLoginViewController.h"
#import "RuleViewController.h"
#import "AppDelegate.h"
#import <IDLFaceSDK/FaceSDKManager.h>
#import "LivenessViewController.h"
#import "LivingConfigModel.h"
#import "ChangeRoleViewController.h"
#import "RegistFViewController.h"
#import "HostRegiViewController.h"
#import "HostLoginViewController.h"

@interface ComenLoginViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *headerV;
@property (weak, nonatomic) IBOutlet UIButton *Agren_Btn;
@property (weak, nonatomic) IBOutlet UILabel *role_Name;
@property(nonatomic,strong)MBProgressHUD *HUD;
@property (nonatomic,assign) RoleType roleType;


@end

@implementation ComenLoginViewController

- (instancetype)initWithRoleType:(RoleType)roleType
{
    if(self = [super init]){
        _roleType = roleType;
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.tableHeaderView = self.headerV;
    [self setupNameWithRoleType:_roleType];
    [self setAttText];

}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


#pragma mark--人脸登录
- (IBAction)face_Action:(UIButton *)sender
{
    if(_roleType==RoleType_GHS || _roleType==RoleType_SJ){
        [WToast showWithText:@"暂不支持人脸登录"];
        return;
    }
    LivenessViewController* lvc = [[LivenessViewController alloc] init];
    LivingConfigModel* model = [LivingConfigModel sharedInstance];
    NSArray *arr = @[@(FaceLivenessActionTypeLiveMouth)];
    model.isByOrder = YES;
    [model.liveActionArray addObjectsFromArray:arr];
    [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
    lvc.completion=^(NSDictionary *images, UIImage *originImage){
        if([images[@"bestImage"] count] > 0){
            NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
            NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            //self.base_Str = base_Str;
            [self uploadFace:base_Str];
            NSLog(@"base-------%@",base_Str);
        }
    };
    [self pushSecondVC:lvc];
}



#pragma mark--注册
- (IBAction)regist_Action:(UIButton *)sender
{
    switch (_roleType) {
        case RoleType_YH:{
            RegistFViewController *vc = [[RegistFViewController alloc] init];
            [self pushSecondVC:vc];
            break;
        }
        case RoleType_ZB:{
            HostRegiViewController *vc = [[HostRegiViewController alloc] init];
            [self pushSecondVC:vc];
            break;
        }
        case RoleType_SJ:
            [WToast showWithText:@"商家暂不支持注册"];
            return;
            break;
        case RoleType_GHS:
            [WToast showWithText:@"供货商暂不支持注册"];
            return;
            break;
    }
}



#pragma mark--验证码登录
- (IBAction)yzm_Login:(UIButton *)sender {
    [WToast showWithText:@"暂不支持，请选择其他方式登录"];
}

#pragma mark--密码登录
- (IBAction)pass_Login:(UIButton *)sender
{
    HostLoginViewController *VC= [[HostLoginViewController alloc] initWithRoleType:_roleType];
    [self pushSecondVC:VC];
}


#pragma mark--切换身份
- (IBAction)change_Role:(UIButton *)sender {
    ChangeRoleViewController *vc = [[ChangeRoleViewController alloc]init];
    vc.islogin = 1;
    vc.selectRole = ^(RoleType type) {
        self.roleType = type;
        [self setupNameWithRoleType:type];
    };
    [self pushSecondVC:vc];
}


- (void)setupNameWithRoleType:(RoleType)type
{
    switch (type) {
        case RoleType_YH:
            self.role_Name.text = @"用户登录";
            break;
        case RoleType_ZB:
            self.role_Name.text = @"主播登录";
            break;
        case RoleType_SJ:
            self.role_Name.text = @"商户登录";
            break;
        case RoleType_GHS:
            self.role_Name.text = @"供货商登录";
            break;
    }
}



-(void)setAttText
{
    NSString *price_text = @"点击登录即代表您同意";
    NSDictionary *attrDict1 = @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Light" size: 13],
                                NSForegroundColorAttributeName:[UIColor grayColor] };
    NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [price_text substringWithRange: NSMakeRange(0, 10)] attributes: attrDict1];
    NSString *price_text1 = @"《板扎秀秀直播》相关协议";
    NSDictionary *attrDict = @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Light" size: 13],
                               NSForegroundColorAttributeName:RGB(47, 174, 255) };
    NSAttributedString *attrStr2 = [[NSAttributedString alloc] initWithString: [price_text1 substringWithRange: NSMakeRange(0, price_text1.length)] attributes: attrDict];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithAttributedString:attrStr1];
    [str appendAttributedString:attrStr2];
    [self.Agren_Btn setAttributedTitle:str forState:UIControlStateNormal];
}



#pragma mark--上传人脸
-(void)uploadFace:(NSString *)base64
{
    NSString *roles = @"";
    switch (_roleType) {
        case RoleType_YH:
            roles = @"live";
            break;
        case RoleType_ZB:
            roles = @"live_anchor";
            break;
        case RoleType_SJ:
            
            break;
        case RoleType_GHS:
            
            break;
    }
   

    NSDictionary *param = @{@"image":base64,@"ta":@"face_login",@"op":@"login",@"ctrl":roles,@"ac":@"auth"};
    [self weqPostHandle:param success:^(id response) {
       
        if(response){
            [WToast showWithText:@"登录成功"];
            id datas = [NSDictionary changeType:response[@"data"]];
            [self loginSuccessWithUserInfo:datas];
        }
        
    } failure:^(NSError *error) {
        
    } hasProgress:1];
}



#pragma mark--登录成功后的处理
- (void)loginSuccessWithUserInfo:(id)userInfo
{
    [XTool SaveDefaultInfo:@(_roleType) Key:Prole];
    switch (_roleType) {
        case RoleType_YH:
            [XTool SaveDefaultInfo:userInfo Key:USER_INFO];
            break;
        case RoleType_ZB:
            [XTool SaveDefaultInfo:userInfo Key:HOST_USER];
            [[NSNotificationCenter defaultCenter] postNotificationName:STOPTIMENOTO object:nil];
            break;
        case RoleType_SJ:
            [XTool SaveDefaultInfo:userInfo Key:BUSSION_INFO];
            [[NSNotificationCenter defaultCenter] postNotificationName:STOPTIMENOTO object:nil];
            break;
        case RoleType_GHS:
            [XTool SaveDefaultInfo:userInfo Key:G_BUSSION_INFO];
            [[NSNotificationCenter defaultCenter] postNotificationName:STOPTIMENOTO object:nil];
            break;
    }
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app changeRootWithRoleType:_roleType];
}



- (IBAction)checkRule:(UIButton *)sender
{
    RuleViewController *vc = [[RuleViewController alloc]init];
    [self pushSecondVC:vc];
}

@end
